
//
//  AddressManageVC.m
//  Ufan
//
//  Created by yangqingluo on 15/12/9.
//  Copyright © 2015年 yangqingluo. All rights reserved.
//

#import "AddressManageVC.h"
#import "AddressManageCell.h"
#import "AddressEditVC.h"

@interface AddressManageVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UIButton *manageButton;
@property (nonatomic, strong) UITableView *showTableView;
@property (nonatomic, strong) UIView *bottomBar;
@property (nonatomic, strong) NSMutableArray *addressArray;

@end

@implementation AddressManageVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.addressArray = [NSMutableArray new];
    [self.showTableView reloadData];
    
    [self getAddresses];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupBarButtonItem];
    [self.view addSubview:self.bottomBar];
}

- (void)setupBarButtonItem{
    self.title = @"选择收货地址";
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 64, 44)];
    [backButton setImage:[UIImage imageNamed:@"nav_back.png"] forState:UIControlStateNormal];
    [backButton setImageEdgeInsets:UIEdgeInsetsMake(12, 0, 12, 48)];
    backButton.tag = 988;
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [self.navigationItem setLeftBarButtonItem:backItem];
    
    
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithCustomView:self.manageButton];
    [self.navigationItem setRightBarButtonItem:saveItem];
}

-(void)goBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBarButtonClick:(UIButton *)button{
    button.selected = !button.selected;
    [self.showTableView setEditing:button.selected animated:YES];
}

- (void)addNewAddress{
    AddressEditVC *aeVC = [[AddressEditVC alloc]initWithAddress:nil];
    [self.navigationController pushViewController:aeVC animated:YES];
}

- (void)getAddresses{
    [self showHudInView:self.view hint:nil];
    [QKHttpManager getAddressCompletion:^(NSObject *object, NSError *error){
        [self hideHud];
        if ([object isKindOfClass:[NSDictionary class]]) {
            NSDictionary *respDic = (NSDictionary *)object;
            if ([respDic[@"code"] intValue] == QKHTTP_SUCCESS) {
                self.addressArray = [NSMutableArray arrayWithArray:addressesFromArray(respDic[@"addressList"])];
                [self.showTableView reloadData];
                
                return;
            }
        }
        
        [self showHint:httpRespString(error, object)];
    }];
}

#pragma response
- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    if ([eventName isEqualToString:UF_Router_Event_AddressSelect]) {
        AddressEditVC *aeVC = [[AddressEditVC alloc]initWithAddress:userInfo[@"info"]];
        [self.navigationController pushViewController:aeVC animated:YES];
    }
}

#pragma getter
- (UITableView *)showTableView{
    if (!_showTableView) {
        _showTableView= [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:UITableViewStylePlain];
        _showTableView.multipleTouchEnabled = YES;
        _showTableView.dataSource = self;
        _showTableView.delegate = self;
        _showTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_showTableView setBackgroundColor:[UIColor clearColor]];
        
        [self.view insertSubview:_showTableView belowSubview:self.bottomBar];
    }
    
    return _showTableView;
}

- (UIView *)bottomBar{
    if (!_bottomBar) {
        _bottomBar = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.height - TAB_BAR_HEIGHT - STATUS_BAR_HEIGHT, self.view.width, TAB_BAR_HEIGHT)];
        _bottomBar.backgroundColor = [UIColor whiteColor];
        _bottomBar.layer.shadowColor = [UIColor blackColor].CGColor;
        _bottomBar.layer.shadowOffset = CGSizeMake(0, -1);
        _bottomBar.layer.shadowOpacity = 0.2;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 200, 36);
        button.center = CGPointMake(0.5 * _bottomBar.width, 0.5 * _bottomBar.height);
        button.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [button setBackgroundColor:[UIColor clearColor]];
        [button setTitle:@"＋新地址" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(addNewAddress) forControlEvents:UIControlEventTouchUpInside];
        [_bottomBar addSubview:button];
    }
    
    return _bottomBar;
}

- (UIButton *)manageButton{
    if (!_manageButton) {
        _manageButton = [[UIButton alloc] initWithFrame:CGRectMake(self.navigationController.navigationBar.width - 64, 0, 64, 44)];
        [_manageButton setTitle:@"管理" forState:UIControlStateNormal];
        [_manageButton setTitle:@"完成" forState:UIControlStateSelected];
        _manageButton.titleLabel.font = [UIFont systemFontOfSize:16.0];
        _manageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _manageButton.tag = 989;
        [_manageButton addTarget:self action:@selector(rightBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _manageButton;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.addressArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"AddressManageCell";
    AddressManageCell *cell = (AddressManageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[AddressManageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.address = self.addressArray[indexPath.row];
    
    return cell;
}


#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [AddressManageCell tableView:tableView heightForRowAtIndexPath:indexPath];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.manageButton.selected) {
        NSLog(@"****");
    }
    else{
        [AppPublic shareInstanceApp].addressSelected = self.addressArray[indexPath.row];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return UITableViewCellEditingStyleDelete;
//}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UFAddressInfo *address = self.addressArray[indexPath.row];
        
        if (address.def) {
            
        }
        else{
            [self showHudInView:self.view hint:@"删除中..."];
            [QKHttpManager deleteAddress:address.idString completion:^(NSObject *object, NSError *error){
                [self hideHud];
                if ([object isKindOfClass:[NSDictionary class]]) {
                    NSDictionary *respDic = (NSDictionary *)object;
                    if ([respDic[@"code"] intValue] == QKHTTP_SUCCESS) {
                        [self.addressArray removeObject:address];
                        [self.showTableView reloadData];
                        
                        return;
                    }
                    
                    
                }
                
                [self showHint:httpRespString(error, object)];
            }];
        }
        
    }
    
}

@end
